package com.galleryviewpager.b_wave;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.galleryviewpager.DensityUtils;
import com.galleryviewpager.R;

public class WaveView extends View {

    private int mWaveCount;
    private int mWaveWidth;
    private int mMode;
    private int mColor;

    private int mWidth;
    private int mHeight;
    private Context mContext;

    private Paint mPaint = new Paint();

    private RectF rectF = new RectF();
    private int mRectWidth, mRectHeigth;
    private int mWaveHeight;

    //圆角模式
    public final int MODE_CIRCLE = -1;

    //三角模式
    public final int MODE_TRIANGLE = -2;

    private Path mPath = new Path();

    private RectF cicleRectF = new RectF();

    public WaveView(Context context) {
        this(context, null);
    }

    public WaveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WaveView, defStyleAttr, 0);

        mWaveCount = typedArray.getInt(R.styleable.WaveView_waveCount, 10);
        mWaveWidth = typedArray.getInt(R.styleable.WaveView_waveWidth, 20);
        mMode = typedArray.getInteger(R.styleable.WaveView_mode, -2);
        mColor = typedArray.getColor(R.styleable.WaveView_android_color, Color.RED);
        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        switch (widthMode) {
            case MeasureSpec.AT_MOST:
                mWidth = DensityUtils.dip2px(mContext, 300);
                break;
            case MeasureSpec.EXACTLY:
                mWidth = widthSize;
                break;
        }
        mRectWidth = (int) (mWidth * 0.8);

        switch (heightMode) {
            case MeasureSpec.AT_MOST:
                mHeight = DensityUtils.dip2px(mContext, 200);
                break;
            case MeasureSpec.EXACTLY:
                mHeight = heightSize;
                break;
        }
        mRectHeigth = (int) (mHeight * 0.8);

        rectF.set(0, 0, mRectWidth, mRectHeigth);
        setMeasuredDimension(mWidth, mHeight);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPath.reset();

        mPaint.setColor(mColor);

        mWaveHeight = mRectHeigth / mWaveCount;

        int padding = (mWidth - mRectWidth) / 2;
        canvas.drawRect(padding, padding, mRectWidth + padding, mRectHeigth + padding, mPaint);
        if (mMode == MODE_TRIANGLE) {
            float startX = mRectWidth + padding;
            float startY = padding;
            mPath.moveTo(startX, startY);
            for (int i = 0; i < mWaveCount; i++) {
                mPath.lineTo(startX + mWaveWidth, startY + i * mWaveHeight + mWaveHeight / 2);
                mPath.lineTo(startX, startY + mWaveHeight * (i + 1));
            }
            mPath.close();
            canvas.drawPath(mPath, mPaint);
            mPath.reset();
            startX = padding;
            startY = padding;
            mPath.moveTo(startX, startY);
            for (int i = 0; i < mWaveCount; i++) {
                mPath.lineTo(startX - mWaveWidth, startY + mWaveHeight / 2 + (mWaveHeight * (i)));
                mPath.lineTo(startX, startY + mWaveHeight * (i + 1));
            }
            mPath.close();
            canvas.drawPath(mPath, mPaint);
        } else {  //圆形

            float startX = padding + mRectWidth;
            float startY = padding;
            mPath.moveTo(startX, startY);
            for (int i = 0; i < mWaveCount; i++) {
                float left = startX - mWaveWidth;
                float top = startY + mWaveHeight * i;
                float right = startX + mWaveWidth;
                float bottom = startY + mWaveHeight * (i + 1);
                cicleRectF.set(left, top, right, bottom);
                mPath.addArc(cicleRectF, -90, 180);
            }
            mPath.close();
            mPaint.setColor(Color.GREEN);
            canvas.drawPath(mPath, mPaint);

            mPath.reset();
            startX = startY = padding;
            mPath.moveTo(startX, startY);
            for (int i = 0; i < mWaveCount; i++) {
                float left = startX - mWaveWidth;
                float top = startY + i * mWaveHeight;

                float right = startX + mWaveWidth;
                float bottom = startY + (i + 1) * mWaveHeight;
                cicleRectF.set(left, top, right, bottom);
                mPath.addArc(cicleRectF, 90, 180);
            }
            mPath.close();
            mPaint.setColor(Color.BLUE);
            canvas.drawPath(mPath, mPaint);
        }
    }
}
